package com.usc.csci588.chaltrojan.location.map;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

/**
 * https://developers.google.com/maps/documentation/android/hello-mapview
 * 
 * This is to add overlays and markers at the top of the MapView that is visible to us
 * 
 * @author jaspreet
 *
 */

public class CTItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	
	public CTItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}
	
	public CTItemizedOverlay(Drawable defaultMarker, Context context) {
		  super(boundCenterBottom(defaultMarker));
		  mContext = context;
		}

	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}
	
	/**
	 * When the populate() method executes, it will call createItem(int) 
	 * in the ItemizedOverlay to retrieve each OverlayItem. 
	 * You must override this method to properly read from the ArrayList
	 *  and return the OverlayItem from the position specified by the given integer.
	 */
	@Override
	protected OverlayItem createItem(int i) {
		  return mOverlays.get(i);
	}

	@Override
	public int size() {
		 return mOverlays.size();
	}
	
	/**
	 * the onTap() callback method, which will handle the event when an item is tapped by the user
	 */
	@Override
	protected boolean onTap(int index) {

		/**
		 * Figure out a way to use this to select the location
		 * that is destination and display the route 
		 */
		/*
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		
		
		Log.d("maps", "I am insisde onTap function..");
		
		Toast.makeText(mContext, "You tapped "+mOverlays.get(index).toString(), Toast.LENGTH_LONG).show();
		*/
		
		/**
		 * This is default one 
		 */
		/*
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		//*/
		return true;
	}

	@Override
	public boolean onTap(GeoPoint p, MapView map){
		Log.d("maps", "I am insisde onTap function with point p and map m.. Started from : "+mOverlays.get(0).getPoint().toString()+ " TRYING TO GO TO "+p.toString());

		///*

		GeoPoint startPoint = mOverlays.get(0).getPoint();
		
		StringBuilder urlString = new StringBuilder("");
		urlString.append("http://maps.google.com/maps?saddr=");
    	urlString.append(Double.toString(startPoint.getLatitudeE6()/ (double)1E6));
    	urlString.append(",");
    	urlString.append(Double.toString(startPoint.getLongitudeE6()/ (double)1E6));
    	urlString.append("&daddr=");// to
    	urlString.append(Double.toString(p.getLatitudeE6()/ (double)1E6));
    	urlString.append(",");
    	urlString.append(Double.toString(p.getLongitudeE6()/ (double)1E6));
		
		try {
			
			Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(urlString.toString()));
			mContext.startActivity(intent);			
        } catch (ActivityNotFoundException anfe) {
        	
        }
        
	    //*/
		return true;
	}
	
	
	
}
